<template>
  <div id="app">
    <router-view v-if="isRouterAlive" />
  </div>
</template>

<script>
export default {
  name: 'App',
  provide() {
    return {
      reload: this.reload,
      goUrl: this.goUrl,
      isEdit: this.isEdit,
      add: this.add,
      update: this.update,
      eqStatusFormatter: this.eqStatusFormatter,
      planCheck: this.planCheck,
      paramCategory: this.paramCategory,
      applyStatus: this.applyStatus,
      orderStatus: this.orderStatus,
      enterStatus: this.enterStatus,
      toolEnterTypes: this.toolEnterTypes,
      toolEnterStatus: this.toolEnterStatus,
      toolAppearStatus: this.toolAppearStatus,
      toolAppearTypes: this.toolAppearTypes,
      costStatus: this.costStatus,
      providerIs: this.providerIs
    }
  },
  data() {
    return {
      isRouterAlive: true
    }
  },
  methods: {
    reload() {
      this.isRouterAlive = false
      this.$nextTick(function() {
        this.isRouterAlive = true
      })
    },
    goUrl(url) {
      this.$router.push(url)
    },
    isEdit() {
      return Number(this.$route.params.id) || false
    },
    add(API, thisData, thisForm, url) {
      thisForm.validate((valid) => {
        if (valid) {
          API.create(thisData).then((res) => {
            const resId = res.data.res
            if (res.data.error_code === 1) {
              this.$message({
                message: '恭喜你，新增成功',
                type: 'success'
              })
              if (url) {
                this.goUrl(url + resId)
              } else {
                this.$router.go(-1)
              }
            } else {
              this.$message.error(res.data.message)
            }
          })
        }
      })
    },
    update(API, thisData, thisForm, url) {
      thisForm.validate((valid) => {
        if (valid) {
          API.update(thisData).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              if (url) {
                this.goUrl(url)
              } else {
                this.$router.go(-1)
              }
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    planCheck(cellValue) {
      if (cellValue === 0) {
        return this.$createElement('span', { style: { 'color': '#333' }}, '否')
      } else if (cellValue === 1) {
        return this.$createElement('span', { style: { 'color': '#333' }}, '是')
      }
    },
    providerIs(cellValue) {
      if (cellValue === 2) {
        return this.$createElement('span', { style: { 'color': '#333' }}, '否')
      } else if (cellValue === 1) {
        return this.$createElement('span', { style: { 'color': '#333' }}, '是')
      }
    },
    paramCategory(row) {
      if (row.category === 0) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '测量')
      } else if (row.category === 1) {
        return this.$createElement('span', { style: { 'color': 'blue' }}, '增量型的')
      } else if (row.category === 2) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '非增量型')
      } else if (row.category === 3) {
        return this.$createElement('span', { style: { 'color': '#F56C6C' }}, '感官')
      }
    },
    applyStatus(row) {
      if (row.status === 0) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '创建')
      } else if (row.status === 1) {
        return this.$createElement('span', { style: { 'color': 'red' }}, '待审核（工程师）')
      } else if (row.status === 2) {
        return this.$createElement('span', { style: { 'color': 'red' }}, '待审核（科长）')
      } else if (row.status === 3) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '申请完成')
      } else if (row.status === 4) {
        return this.$createElement('span', { style: { 'color': 'red' }}, '待审核（工程师）')
      } else if (row.status === 5) {
        return this.$createElement('span', { style: { 'color': 'red' }}, '待审核（科长）')
      } else if (row.status === 6) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '入库完成')
      } else if (row.status === 7) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '归档')
      }
    },
    orderStatus(row) {
      if (row.status === 0) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '创建')
      } else if (row.status === 1) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '审核完成')
      } else if (row.status === 2) {
        return this.$createElement('span', { style: { 'color': 'red' }}, '退回')
      } else if (row.status === 3) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '入库完成')
      } else if (row.status === 4) {
        return this.$createElement('span', { style: { 'color': 'red' }}, '归档')
      }
    },
    enterStatus(row) {
      if (row.status === 0) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '创建')
      } else if (row.status === 1) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '入库完成')
      } else if (row.status === 2) {
        return this.$createElement('span', { style: { 'color': 'red' }}, '归档')
      }
    },
    toolEnterTypes(row) {
      if (row.type === 0) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '采购')
      } else if (row.type === 1) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '赠予')
      } else if (row.type === 2) {
        return this.$createElement('span', { style: { 'color': 'red' }}, '自制')
      } else if (row.type === 3) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '修旧利废')
      }
    },
    toolEnterStatus(row) {
      if (row.status === 0) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '创建')
      } else if (row.status === 1) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '入库完成')
      } else if (row.status === 2) {
        return this.$createElement('span', { style: { 'color': 'red' }}, '归档')
      }
    },
    toolAppearStatus(row) {
      if (row.status === 0) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '创建')
      } else if (row.status === 1) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '出库完成')
      } else if (row.status === 2) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '归档')
      } else if (row.status === 3) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '已归还')
      }
    },
    toolAppearTypes(row) {
      if (row.kind === 0) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '领用')
      } else if (row.kind === 1) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '借用')
      } else if (row.kind === 2) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '以旧换新')
      }
    },
    costStatus(row) {
      if (row.status === 0) {
        return this.$createElement('span', { style: { 'color': 'grey' }}, '测试')
      } else if (row.status === 1) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '启用')
      } else if (row.status === 2) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '临时移除')
      } else if (row.status === 3) {
        return this.$createElement('span', { style: { 'color': 'green' }}, '停用')
      }
    },
    showSize(base64url) {
      let str = base64url.replace('data:image/png;base64,', '')
      let equalIndex = str.indexOf('=')
      if (str.indexOf('=') > 0) {
        str = str.substring(0, equalIndex)
      }
      let strLength = str.length
      let fileLength = parseInt(strLength - (strLength / 8) * 2)
      // 由字节转换为kb
      let size = ''
      size = (fileLength / 1024).toFixed(2)
      let sizeStr = size + '' //转成字符串
      let index = sizeStr.indexOf('.') //获取小数点处的索引
      let dou = sizeStr.substr(index + 1, 2) //获取小数点后两位的值
      if (dou == '00') { //判断后两位是否为00，如果是则删除00
        return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
      }
      return size
    },
  }
}
</script>
